package com.lonwin.mes2u8.service;


import com.lonwin.mes2u8.controller.MaterialOutController;
import com.lonwin.mes2u8.controller.ProductionController;
import com.lonwin.mes2u8.controller.SaleOrderController;
import com.lonwin.mes2u8.domain.mes.MesBatchCodeInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

@Component
@Slf4j
public class MyTimerService {
    @Resource
    private BatchCodeService batchCodeService;
    @Resource
    private SaleOrderController saleOrderController;
    @Resource
    private MaterialOutController materialOutController;
    @Resource
    private ProductionController productionController;

    @Scheduled(cron = "0 0 1 * * ? ")
    public void execTimerDateCollect() {
        log.info("timer 1 : translate data from MES to U8 start");
        log.info("timer 2 : start get batch code list");
        List<MesBatchCodeInfo> mesBatchCodeInfoList = batchCodeService.queryMesBatchList();

        if (mesBatchCodeInfoList.isEmpty()) {
            log.info("batch code list is empty");
            return;
        }

        List<String> batchCodeList = mesBatchCodeInfoList.stream().map(MesBatchCodeInfo::getBno)
                .filter(s -> !s.startsWith("22") && !s.startsWith("23")).collect(Collectors.toList());

        log.info("timer 3 : get batch code list success, totle {}", batchCodeList.size());
        log.info("timer 4 : start translate sale order");
        saleOrderController.addSaleOrder(batchCodeList);
        log.info("timer 5 : translate sale order finshed");
//        log.info("timer 6 : start translate material out");
//        materialOutController.addMaterial(batchCodeList);
//        log.info("timer 7 : translate material out finshed");
        log.info("timer 8 : start translate production ");
        productionController.addProductions(batchCodeList);
        log.info("timer 9 : translate production finshed");
        log.info("timer 10 : translate data from MES to U8 end");
    }
}
